package org.molamil.acto.core.parsers {
	import org.molamil.acto.types.NullTypeParser;	
	
	import flexunit.framework.TestCase;	
	
	/**
	 * @author Jorge Hernandez
	 */
	public class PrioritizableListTest extends TestCase {
		
		
		// CONSTRUCTOR
		
		public function PrioritizableListTest(methodName:String = null) {
			super(methodName);
		}
		
		
		// PUBLIC METHODS
		
		public function testAdd():void {
			
			var instance:PrioritizableList = new PrioritizableList();
			
			assertTrue("a is an array", instance.a is Array);			
			assertTrue("a has zero elements", instance.a.length == 0);
			
			// Add one element.
			var p:Prioritizable = new NullTypeParser();
			instance.add(p);
			assertTrue("a has 1 element", instance.a.length == 1);
			
			// Add another element.
			instance.add(new NullTypeParser());
			assertTrue("a has 2 elements", instance.a.length == 2);
			
			// Add the first element again.
			instance.add(p);
			assertTrue("a has 2 elements after reinserting first", instance.a.length == 2);
			
			// Add null.
			instance.add(null);
			assertTrue("a has 2 elements after inserting null", instance.a.length == 2);
			
		}
		
		public function testRemove():void {
			
			var instance:PrioritizableList = new PrioritizableList();
			
			var p:Prioritizable = new NullTypeParser();
			instance.add(p);
			instance.add(new NullTypeParser());
			instance.add(new NullTypeParser());
			
			assertTrue("a has 3 elements", instance.a.length == 3);
			
			instance.remove(p);			
			assertTrue("a has 2 elements after removing the first", instance.a.length == 2);
			
			instance.remove(p);			
			assertTrue("a has 2 elements after removing the first again", instance.a.length == 2);
			
			instance.remove(new NullTypeParser());			
			assertTrue("a has 2 elements after removing an element not in the list", instance.a.length == 2);
			
			instance.remove(null);			
			assertTrue("a has 2 elements after removing null", instance.a.length == 2);
			
		}
		
		public function testContains():void {
			
			var instance:PrioritizableList = new PrioritizableList();
			
			assertTrue("a doesn't contain null when empty", !instance.contains(null));
			assertTrue("a doesn't contain an item when empty", !instance.contains(new NullTypeParser()));
			
			var p:Prioritizable = new NullTypeParser();
			instance.add(p);
			instance.add(new NullTypeParser());
			instance.add(new NullTypeParser());
			
			assertTrue("a doesn't contain null", !instance.contains(null));
			assertTrue("a contains the first element", instance.contains(p));
			assertTrue("a doesn't contain a new item", !instance.contains(new NullTypeParser()));
			
			instance.remove(p);
			assertTrue("a doesn't contain the first element after removing", !instance.contains(p));
			
		}
		
		public function testSort():void {
			
			var instance:PrioritizableList = new PrioritizableList();
			
			var p100:Prioritizable = new NullTypeParser();
			p100.priority = 100;
			var p150:Prioritizable = new NullTypeParser();
			p150.priority = 150;
			var p125:Prioritizable = new NullTypeParser();
			p125.priority = 125;
			var p2000:Prioritizable = new NullTypeParser();
			p2000.priority = 2000;
			var p10:Prioritizable = new NullTypeParser();
			p10.priority = 10;
			var p15:Prioritizable = new NullTypeParser();
			p15.priority = 15;
			
			instance.add(p100);
			instance.add(p150);
			instance.add(p125);
			instance.add(p2000);
			instance.add(p10);
			instance.add(p15);
			
			instance.sort();
			
			assertTrue("a holds the same size", instance.a.length == 6);
			assertTrue("a[0] is right", instance.a[0] == p10);
			assertTrue("a[1] is right", instance.a[1] == p15);
			assertTrue("a[2] is right", instance.a[2] == p100);
			assertTrue("a[3] is right", instance.a[3] == p125);
			assertTrue("a[4] is right", instance.a[4] == p150);
			assertTrue("a[5] is right", instance.a[5] == p2000);
			
		}
		
		public function testClear():void {
			
			var instance:PrioritizableList = new PrioritizableList();
			
			instance.clear();
			assertTrue("a is empty after clearing when empty", instance.a.length == 0);
			
			var p:Prioritizable = new NullTypeParser();
			instance.add(p);
			instance.add(new NullTypeParser());
			instance.add(new NullTypeParser());
			
			instance.clear();
			assertTrue("a is empty after clearing", instance.a.length == 0);
			
			instance.add(new NullTypeParser());
			
			instance.clear();
			assertTrue("a is empty after clearing after refilling", instance.a.length == 0);
			
		}
		
		
	}
	
}
